package com.xbai.intervention.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.xbai.intervention.client.DeviceMapper;
import com.xbai.intervention.client.DeviceServiceClient;
import com.xbai.intervention.client.UserMapper;
import com.xbai.intervention.client.UserServiceClient;
import com.xbai.intervention.domain.Intervention;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author jxbai\
 * @date 2021/1/15 0015
 */
@Service
@Slf4j
public class InterventionService {

    @Autowired
    private UserServiceClient userServiceClient;
    @Autowired
    private DeviceServiceClient deviceServiceClient;

    public Intervention generateIntervention(String userName, String deviceCode) {
        log.info("Generate intervention record with user: {} from device: {}",
                userName, deviceCode);
        Intervention intervention = new Intervention();
        UserMapper user = getUser(userName);
        if (user == null) {
            return intervention;
        }
        log.info("Get remote user: {} is successful", userName);
        DeviceMapper device = getDevice(deviceCode);
        if (device == null) {
            return intervention;
        }
        log.info("Get remote device: {} is successful", deviceCode);
        //创建并保存Intervention信息
        intervention.setUserId(user.getId());
        intervention.setDeviceId(device.getId());
        intervention.setHealthData(device.getHealthData());
        intervention.setIntervention("InterventionForDemo");
        intervention.setCreateTime(new Date());
        return intervention;
    }


    private UserMapper getUser(String userName) {
        return userServiceClient.getUserByUserName(userName);
    }




    private DeviceMapper getDevice(String deviceCode) {
        return deviceServiceClient.getDeviceByDeviceCode(deviceCode);
    }
}
